// pages/equipment/configuration/configuration.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    hospInfo: null,
    custom:true,
    options: [{
        label: "常规催乳",
        selected: false
      },
      {
        label: "乳汁分泌少",
        selected: false
      },
      {
        label: "乳腺管不通",
        selected: false
      },
      {
        label: "子宫复旧",
        selected: false
      },
      {
        label: "产后排尿",
        selected: false
      },

      {
        label: "产后恢复",
        selected: false
      },
      {
        label: "治疗项目1",
        selected: false
      },
      {
        label: "治疗项目2",
        selected: false
      },
    ],
    selected: [],
    day: '',
    hour: '',
    days: [{
        label: '3天',
        value: '3'
      },
      {
        label: '6天',
        value: '6'
      },
      {
        label: '7天',
        value: '7'
      },
      {
        label: '10天',
        value: '10'
      },
      {
        label: '15天',
        value: '15'
      }
    ],
    hours: [{
        label: '1小时',
        value: '1'
      },
      {
        label: '1.5小时',
        value: '1.5'
      },
      {
        label: '2小时',
        value: '2'
      },
      {
        label: '3小时',
        value: '3'
      },
    ],
    hours1: [{
        label: '1小时',
        value: '1'
      },
      {
        label: '1.5小时',
        value: '1.5'
      },
      {
        label: '2小时',
        value: '2'
      },
      {
        label: '3小时',
        value: '3'
      },
    ],
    hours2: [{
        label: '1小时',
        value: '1'
      },
      {
        label: '1.5小时',
        value: '1.5'
      },
      {
        label: '2小时',
        value: '2'
      },
    ],
    checked: false,
    showDialog: false,
    disabled: true,
    isLock:null
  },
  onLoad() {
    // console.log(app.globalData.userInfo, '个人信息');
  },
  onShow() {
    wx.setStorageSync('isT',0)
    this.getList()
    this.getServeList()
    this.setData({
      disabled: true,
      checked:false,
      isLock:null
    })
  },
  getList() {
    const that = this
    if (app.globalData.userInfo == '') {
      app.getUserInfo().then(res => {
        that.setData({
          userInfo: res
        })
      })
    } else {
      that.setData({
        userInfo: app.globalData.userInfo
      })
    }
    app.getHost(that.data.userInfo.hospitalId).then(res => {
      that.setData({
        hospInfo: res,
        custom:res.settlementUnit==2?false:true
      })
    })
  },
  getServeList(){
    let that = this
    wx.request({
      url: app.globalData.serverUrl+'/treatment/list',
      method:"GET",
      header:{
        'token':wx.getStorageSync('token')
      },
      success:res=>{
        let resData= res.data
        if(resData.code==200){
          resData.data.regionList.map(item=>{
            item.selected = false
          })
          that.setData({
            options:resData.data.regionList
          })
        }
      }
    })
  },
  showModal(e) {
    const id = e.currentTarget.dataset.id
    if (id == 'modalHours' && !this.data.day) {
      return app.showMsg('请选择该项目的总天数')
    }
    const modal = this.selectComponent('#' + id)
    modal.show()
  },
  bindChangeDays(e) {
    let value = e.detail.value
    this.setData({
      day: e.detail.value,
      hours: value <= 7 ? this.data.hours1 : this.data.hours2
    })
    this.updateDisabled()
  },
  bindChangeHours(e) {
    this.setData({
      hour: e.detail.value
    })
    this.updateDisabled()
  },
  bindSelectAll(e) {
    let checked = !this.data.checked
    let options = this.data.options.map((item) => {
      item.selected = checked
      return item
    })
    this.setData({
      checked: checked,
      options: options
    })
    this.updateDisabled()
  },
  /**
   * 点击治疗项目
   */
  bindtapItem(e) {
    let index = e.currentTarget.dataset.index
    let options = this.data.options
    let selected = !options[index].selected
    options[index].selected = selected

    let count = 0
    this.data.options.forEach(item => {
      if (item.selected) {
        count = count + 1
      }
    })
    this.setData({
      options: options,
      checked: count == options.length
    })
    this.updateDisabled()
  },
  bindtapNext() {
    let that = this
    let lock=null
    if(!that.data.isLock){
      that.setData({
        isLock:new Date().getTime()
      })
    }else{
      lock = new Date().getTime()
    }
    if(that.data.isLock && lock){
      if(lock-that.data.isLock<app.globalData.isNumLock){
        return
      }else{
        that.setData({
          isLock:lock
        })
      }
    }
    console.log(that.data.selected, that.data.day, that.data.hour);
    let sumHours = that.data.hospInfo.settlementUnit==2?(Number(that.data.day)*Number(that.data.hour)):Number(that.data.day)
    let initHours = that.data.hospInfo.settlementUnit==2?that.data.hospInfo.availableHours:that.data.hospInfo.availableDays
    if(initHours<sumHours && that.data.hospInfo.controlType == 0){
      that.setData({
        showDialog: true
      })
    }else{
    let selectedArr = that.data.selected.map(item=>{
      return item.treatmentId
    })
      let ovtData = {
        selected:selectedArr,
        day:Number(that.data.day),
        hour:Number(that.data.hour),
        type:that.data.hospInfo.type
      }
      console.log(ovtData);
      wx.navigateTo({
        url: '/pages/equipment/add/add?ovtData='+JSON.stringify(ovtData),
      })
    }
  },
  updateDisabled() {
    const {
      day,
      hour
    } = this.data
    let selected = this.data.options.filter(item => {
      return item.selected
    })
    this.setData({
      selected: selected,
      disabled: !(day && hour && selected.length > 0)
    })
  },
  hideDialog() {
      this.setData({
        showDialog: false
      })
  }
})